'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');
const fs = require("fs");

const original = [
  "config/index.js",
  "db/index.js",
  "middleware/index.js",
  "models/index.js",
  "public/index.html",
  "public/favicon.ico",
  "routes/index.js",
  "utils/index.js",
  "index.js",
]

module.exports = class extends Generator {
  // 1. 初始化 初始化一些状态之类的，通常是和用户输入的 options 或者 arguments 打交道
  initializing (){}
  // 2. 和用户交互的时候（命令行问答之类的）调用
  prompting() {
    this.log(yosay(`Welcome to the stylish ${chalk.red('generator-koa-2')} generator!`));

    const prompts = [
      {
        type: "input",
        name: "name",
        message: "Your project name?",
        default: this.appname
      },
      {
        type: "input",
        name: "version",
        message: "Your project version?",
        default: "0.0.1"
      },
      {
        type: 'input',
        name: 'author',
        message: 'Your project author',
        default: this.user.git.name()
      },
      {
        type: 'input',
        name: 'email',
        message: 'Your project email',
        default: this.user.git.email()
      },
      {
        type: 'input',
        name: 'description',
        message: 'Your project description',
        default: ''
      },
      // {
      //   type: 'select',
      //   name: '多选',
      //   message: '选择111/222',
      //   options: [{
      //     name: '111',
      //     value: 1
      //   }, {
      //     name: '222',
      //     value: 2
      //   }]
      // },
      {
        type: 'list',
        name: 'database',
        message: 'choice a database and orm framework!',
        default: 'mongodb & mongoose',
        choices: [{
          name: "mongodb & mongoose",
          value:  1
        }, {
          name: "mysql & sequlize",
          value: 2
        }, {
          name: "redis & ioredis",
          value: 3
        }, {
            name: 'none',
            value: false          
        }]
      },
    ];

    return this.prompt(prompts).then(props => {
      this.props = props;
    });
  }

  // 3. 保存配置文件(如 .babelrc 等)
  configuring() {
    const configList = [".gitignore", "README.md"];
    configList.forEach(c => {
      this.fs.copy(this.templatePath(c),this.destinationPath(c));
    });
  }

  // 4. 其他方法都会在这里按顺序统一调用
  default(){}

  // 5. 在这里写一些模板文件
  writing() {
    const { name, version, email, author, description, database } = this.props;
    // 生成package.json
    this.fs.copyTpl(
      this.templatePath('package.json'),
      this.destinationPath('package.json'), {
        name: '"' + name + '"',
        version: '"' + version + '"',
        email: '"' + email + '"',
        author: '"' + author + '"',
        description: '"' + description + '"',
        database,
      }
    );

    original.forEach(src => {
      this.fs.copy(
        this.templatePath(src),
        this.destinationPath(src)
      )
    });
  }

  // 6. 处理冲突
  conflicts(){}

  // 7. 开始安装依赖
  install() {
    this.installDependencies({
      bower: false,
      npm: true,
      yarn: true,
      callback: function(){
        this.log("准备完成...")
      }
    });
  }

  // 8. 擦屁股
  end(){}
};
